package com.aiju.cash.ui.member;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.aiju.app.SimpleHttpCallback;
import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.bean.MPoints;
import com.aiju.cash.R;
import com.aiju.cash.core.api.MemberPointApi;
import com.aiju.utils.T;
import com.aiju.widgit.dialog.PointsDialog;
import com.aiju.widgit.layout.SettingItemTextView;

/**
 * @author: 佐为
 * @date: 2017-02-20 15:59
 * @version: V1.0
 * @Description: 会员积分
 */
public class MemberPointsSettingActivity extends BaseTitleActivity {

    private SettingItemTextView settingItemTextView;
    private MemberPointApi memberPointApi;

    private String status = "0";//会员积分开关
    private String useType = "0";//使用方式 抵用消费金额的10%
    private String pointsWorth = "1";//积分价值，如100积分抵用1元

    private View switchView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_member_point_setting);
        setCenterTitle("设置会员积分");

        useType = mUser.getMPoints() == null ? "0" : mUser.getMPoints().getUseType();
        status = mUser.getMPoints() == null ? "0" : mUser.getMPoints().getStatus();

        switchView = findViewById(R.id.iv_point_switch);
        switchView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                v.setSelected(!v.isSelected());
                status = v.isSelected() ? "1" : "0";
                updata();
            }
        });

        settingItemTextView = (SettingItemTextView) findViewById(R.id.set_points_gravity);
        settingItemTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog();
            }
        });

        if (status.equals("1")) {
            switchView.setSelected(true);
        } else {
            switchView.setSelected(false);
        }
        String sAgeFormat = getResources().getString(R.string.point);
        settingItemTextView.setRightText(String.format(sAgeFormat, useType) + "%");
        memberPointApi = new MemberPointApi(mDialogHelper);
    }

    private void updata() {
        if (TextUtils.isEmpty(useType)) {
            T.ToastShort("请设置使用方式");
            return;
        }
        memberPointApi.update(status, "", useType, new SimpleHttpCallback() {
            @Override
            protected void onDataSuccess(String data) {
                T.ToastShort("设置积分成功");
                MPoints mPoints = null;
                if (mUser.getMPoints() == null) {
                    mPoints = new MPoints();
                } else {
                    mPoints = mUser.getMPoints();
                }
                mPoints.setId(data);
                mPoints.setUseType(useType);
                mPoints.setStatus(status);
                mUser.getShop().setmPoints(mPoints);
            }
        });
    }

    private void showDialog() {
        new PointsDialog(this)
                .setOnItemOnClick(new PointsDialog.OnItemOnClick() {
                    @Override
                    public void click(String item, int position) {
                        useType = item;
                        settingItemTextView.setRightText(item);
                        String sAgeFormat = getResources().getString(R.string.point);
                        settingItemTextView.setRightText(String.format(sAgeFormat, item) + "%");
                        updata();
                    }
                })
                .setCurrentItem(useType)
                .setLayoutParams()
                .show();
    }
}
